<?php

namespace app\result;

use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use think\Request;

class EmailResult extends BaseResult
{

    public function test($args = [])
    {
        (new EmailResult())->send_bill([
            'title' => '测试发送',
            'sub_title' => '',
            'content' => '<h1> 测试邮件 </h1><p> 测试内容</p>',
            'user_id' => $args['user_id'] ?? 0,
            'email' => $args['email']
        ]);
        return self::success_return();
    }

    private function get_dns()
    {
        $config = config('email');
        return 'smtp://' . $config['username'] . ':' . $config['password'] . '@' . $config['host'] . ':' . $config['port'] . '?verify_peer=' . ($config['encryption'] == 'ssl' ? 1 : 0);
    }

    public function send_bill($msgData)
    {
        bs_log($msgData, 'msgData', 'send_bill.log');
        $from = config('email.username');
        $sender = config('email.sender');
        $transport = Transport::fromDsn($this->get_dns());
        $mailer = new Mailer($transport);

        //从缓存中获取发送内容
        if ($msgData['content'] === 'send_bill_leave_msg') {
            $key = $msgData['send_bill_key'];
            $msgData['content'] = cache($key);
            if (empty($msgData['content'])) {
                return $this->error_return('发送失败，请检查发送数据');
            }
            cache($key, null);
        }


        $email = (new Email())
            ->from((new Address($from, $sender)))
            ->to($msgData['email'])
            //->cc('cc@example.com')
            //->bcc('bcc@example.com')
            //->replyTo('fabien@example.com')
            //->priority(Email::PRIORITY_HIGH)
            ->subject($msgData['title'])
            ->text($msgData['sub_title'] ?? '')
            ->html($msgData['content']);
        try {
            $mailer->send($email);
        } catch (TransportExceptionInterface $e) {
            return $this->error_return($e->getMessage());
        }
        return $this->success_return('发送成功');
    }

    //发送验证码
    public function send_code($msgData)
    {
        $from = config('email.username');
        $sender = config('email.sender');
        $transport = Transport::fromDsn($this->get_dns());
        $mailer = new Mailer($transport);
        $email = (new Email())
            ->from((new Address($from, $sender)))
            ->to($msgData['email'])
            //->cc('cc@example.com')
            //->bcc('bcc@example.com')
            //->replyTo('fabien@example.com')
            //->priority(Email::PRIORITY_HIGH)
            ->subject($msgData['title'])
            ->text($msgData['sub_title'] ?? '')
            ->html($msgData['content']);
        try {
            $mailer->send($email);
        } catch (TransportExceptionInterface $e) {
            return $this->error_return($e->getMessage());
        }
        return $this->success_return('发送成功');
    }

    //发送邮件
    public function send_email($msgData)
    {
        $from = config('email.username');
        $sender = config('email.sender');
        $transport = Transport::fromDsn($this->get_dns());
        $mailer = new Mailer($transport);
        $email = (new Email())
            ->from((new Address($from, $sender)))
            ->to($msgData['email'])
            //->cc('cc@example.com')
            //->b
            // cc('bcc@example.com')
            //->replyTo('fabien@example.com')
            //->priority(Email::PRIORITY_HIGH)
            ->subject($msgData['title'])
            ->text($msgData['sub_title'] ?? '')
            ->html($msgData['content']);
        if (!empty($msgData['attachment'])) {
            $email->attachFromPath($msgData['attachment']);
        }
        try {
            $mailer->send($email);
        } catch (TransportExceptionInterface $e) {
            bs_log($e->getMessage(), 'send_email_error', 'send_email_error.log');
            return $this->error_return($e->getMessage());
        }
        return $this->success_return('发送成功');
    }

}
